package com.oracle.lingshangejia.activity;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.NestedScrollView;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.bean.DataBean;
import com.oracle.lingshangejia.bean.DoctorVideoBean;
import com.oracle.lingshangejia.bean.ExpertVideoTitleBean;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;
import com.oracle.lingshangejia.utils.TimeUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

/**
 * 添加接诊
 */
public class TreatPatientActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_title;
    private ImageButton ib_back;
    private EditText et_receptionrecord_Subjectivedata;//就诊者的主观资料
    private EditText et_receptionrecord_Objectivedata; // 就诊者的客观资料
    private EditText et_receptionrecord_Assessment;// 评估
    private EditText receptionrecord_Disposalplan;         // 处置计划
    private EditText receptionrecord_doctorname;
    private TextView receptionrecord_date;
    private Button btn_treat_ok;
    private int jmid;
    private List<String> mOffice = new ArrayList<>(); //科室名字列表
    private List<String> mDoctor = new ArrayList<>();//专家列表
    private Spinner mSpinnerOffice;
    private int mTitleId;
    private ExpertVideoTitleBean mExpertVideoTitleBean;
    private DoctorVideoBean mDoctorVideoBean;
    private Spinner mSpinner;
    private int mDoctorId  = 0;//专家id
    private int mYsid;
    private String mSubjective;
    private String mObjective;
    private String mAssessment;
    private String mDisposalPlan;
    private String mDoctorSign;
    private String mDate;


    @Override
    public int intiLayout() {
        return R.layout.activity_treatpatients;
    }

    @Override
    public void initView() {
        jmid=getIntent().getIntExtra("jmid",0);

        ib_back=findViewById(R.id.ib_back);
        tv_title=findViewById(R.id.tv_title);
        tv_title.setText("添加接诊");
        ib_back.setOnClickListener(this);
        et_receptionrecord_Subjectivedata=findViewById(R.id.receptionrecord_Subjectivedata);
        et_receptionrecord_Objectivedata=findViewById(R.id.receptionrecord_Objectivedata);
        et_receptionrecord_Assessment=findViewById(R.id.receptionrecord_Assessment);
        receptionrecord_Disposalplan=findViewById(R.id.receptionrecord_Disposalplan);
        receptionrecord_doctorname=findViewById(R.id.receptionrecord_doctorname);
        receptionrecord_date=findViewById(R.id.receptionrecord_date);

        receptionrecord_date.setText(TimeUtils.getTodayDate("yyyy-MM-dd"));//获取系统日期
        NestedScrollView nestedScrollView = findViewById(R.id.nsv);




        receptionrecord_date.setOnClickListener(this);
        btn_treat_ok=findViewById(R.id.btn_treat_ok);
        btn_treat_ok.setOnClickListener(this);
        //专家医生列表
        mSpinner = findViewById(R.id.three_doctor);
        mSpinnerOffice = findViewById(R.id.office);
        String name = (String) SpUtils.get(TreatPatientActivity.this,"name"," ");

        receptionrecord_doctorname.setText(name );

    }

    @Override
    public void initData() {
      //  getNetTitleList();
    }


    /**
     * 获取科室列表
     */
    private void getNetTitleList() {
        HashMap<Object,Object> map = new HashMap<>();
        OkHttpUtils.getNetWork(this, Constant.baseurl+"online/ksList",map,handler);
    }

    //设置科室数据
    private void setOffice(ExpertVideoTitleBean titleBean){
        for (ExpertVideoTitleBean.DataBean dataBean :titleBean.getData()){
             mOffice.add(dataBean.getName());

        }
        mSpinnerOffice.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_spinner_dropdown_item,mOffice));
        mSpinnerOffice.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (mOffice.size()>0){
                   mTitleId = mExpertVideoTitleBean.getData().get(position).getId();
                   getNetDoctorList(mTitleId);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void getNetDoctorList(int ksId){
        int ysid= (int) SpUtils.get(TreatPatientActivity.this,"ysid",0);
        HashMap<Object,Object> map = new HashMap<>();
        map.put("ksId",ksId);
        map.put("userId",ysid);//当前登录用户id
        OkHttpUtils.getNetWork(this, Constant.baseurl+"online/sjysList",map,doctor);
    }
    /**
     * 获取科室列表handler
     */
    @SuppressLint("HandlerLeak")
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    String s= (String) msg.obj;
                    Gson gson=new Gson();
                    mExpertVideoTitleBean = gson.fromJson(s,ExpertVideoTitleBean.class);
                    if(mExpertVideoTitleBean.getCode()==0){
                        setOffice(mExpertVideoTitleBean);
                        mTitleId = mExpertVideoTitleBean.getData().get(0).getId();
                        getNetDoctorList(mTitleId);

                    }else{
                        Toast.makeText(TreatPatientActivity.this, mExpertVideoTitleBean.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(TreatPatientActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(TreatPatientActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(TreatPatientActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

     //设置专家列表
    private void setDoctorVideoList(DoctorVideoBean doctorVideoBean) {
             mDoctor.clear();
        for (DoctorVideoBean.DataBean dataBean:doctorVideoBean.getData()){
             mDoctor.add(dataBean.getYsmc());
        }
        mDoctorId =doctorVideoBean.getData().get(0).getId();
        mSpinner.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_spinner_dropdown_item,mDoctor));
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mDoctorId = mDoctorVideoBean.getData().get(position).getId();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });



    }

    /**
     * 医生handler
     */
    private Handler doctor=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    String s= (String) msg.obj;
                    Gson gson=new Gson();
                    mDoctorVideoBean = gson.fromJson(s,DoctorVideoBean.class);
                    if (mDoctorVideoBean.getCode()==0) {
                        setDoctorVideoList(mDoctorVideoBean);
                    }else{
                        Toast.makeText(TreatPatientActivity.this, mDoctorVideoBean.getMsg(),Toast.LENGTH_SHORT).show();
                    }

                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(TreatPatientActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(TreatPatientActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(TreatPatientActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    private Calendar calendar;
    private DatePickerDialog dialog;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_back:
                finish();
                break;
            case R.id.receptionrecord_date:
                calendar = Calendar.getInstance();

                dialog = new DatePickerDialog(TreatPatientActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year,
                                                  int monthOfYear, int dayOfMonth) {
                                String data =  year +"-"+(monthOfYear+1) + "-" + dayOfMonth;
                                receptionrecord_date.setText(data);
                            }

                        }, calendar.get(Calendar.YEAR), calendar
                        .get(Calendar.MONTH), calendar
                        .get(Calendar.DAY_OF_MONTH));
                dialog.show();
                break;
            case R.id.btn_treat_ok:
                // 就诊者的主观资料
                mSubjective = et_receptionrecord_Subjectivedata.getText().toString().trim();
                // 就诊者的客观资料
                mObjective = et_receptionrecord_Objectivedata.getText().toString().trim();
                // 评估
                mAssessment = et_receptionrecord_Assessment.getText().toString().trim();
                // 处置计划
                mDisposalPlan = receptionrecord_Disposalplan.getText().toString().trim();
                // 医生签字
                mDoctorSign = receptionrecord_doctorname.getText().toString().trim();
                // 接诊日期
                mDate = receptionrecord_date.getText().toString().trim();

                if(TextUtils.isEmpty(mSubjective)){
                    Toast.makeText(TreatPatientActivity.this,"请填写就诊者的主观资料",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(mObjective)){
                    Toast.makeText(TreatPatientActivity.this,"请填写就诊者的客观资料",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(mAssessment)){
                    Toast.makeText(TreatPatientActivity.this,"请填写评估",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(mDisposalPlan)){
                    Toast.makeText(TreatPatientActivity.this,"请填写处置计划",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(mDoctorSign)){
                    Toast.makeText(TreatPatientActivity.this,"请填写医生签字",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(mDate)){
                    Toast.makeText(TreatPatientActivity.this,"请选择接诊日期",Toast.LENGTH_SHORT).show();
                    return;
                }/*if (mDoctorId == 0){
                MyToast.show("请选择专家医生");
                }*/
                getNetWork();

                break;
        }
    }




    private void getNetWork() {
        mYsid = (int) SpUtils.get(TreatPatientActivity.this,"ysid",0);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("jmId",jmid); //居民id 从居民档案列表传过来的
        map.put("jzzzgzl", mSubjective);
        map.put("jzzkgzl", mObjective);
        map.put("pg", mAssessment);
        map.put("czjh", mDisposalPlan);
        map.put("ysId", mYsid);//社区医生用户id
      //  map.put("jzrq", mDate);时间
       // map.put("zj_id",mDoctorId );//专家id选中的
        OkHttpUtils.getNetWork(TreatPatientActivity.this, Constant.baseurl+"online/jzzz/jz",map,doctorhandler);

    }

    private Handler doctorhandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:  //成功S
                    String s= (String) msg.obj;
                    Gson gson=new Gson();
                    DataBean bean=gson.fromJson(s, DataBean.class);
                    if(bean.getCode()==0){
                        startActivity(new Intent(TreatPatientActivity.this,TakingRecordsActivity.class));
                        finish();
                    }else{
                        Toast.makeText(TreatPatientActivity.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(TreatPatientActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(TreatPatientActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(TreatPatientActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}
